package com.ocm.tourism.web;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import com.ocm.tourism.core.domain.CulinaryCulture;
import com.ocm.tourism.core.domain.Distance;
import com.ocm.tourism.core.domain.Hotel;
import com.ocm.tourism.core.service.BusinessService;
import com.ocm.tourism.core.service.CataloguesService;
import com.ocm.tourism.web.model.DistanceListModel;

@RequestMapping("/distance")
@Controller
public class DistanceController {

   @Autowired
   CataloguesService cataloguesService;

   @Autowired
   BusinessService businessService;

   @RequestMapping(value = "/{id}/update/form", method = RequestMethod.GET)
   public String updateDistancesForm(@PathVariable("id")Long businessId, Model uiModel) {
      List<Distance> distances = businessService.findDistances(businessId);
      processDistances(businessId,distances,uiModel);
      return "distance/update";
   }

   @RequestMapping(value = "/{id}/update", method=RequestMethod.POST)
   public String updateDistances(@PathVariable("id") Long hotelId, @Valid DistanceListModel distanceListModel, BindingResult bindingResult, Model uiModel) {
      if (bindingResult.hasErrors()) {
         return updateDistancesForm(hotelId, uiModel);
      }
      Hotel hotel = businessService.findhotel(hotelId);
      hotel.addAndUpdateDistances(distanceListModel.getDistances());
      businessService.saveBusiness(hotel);
      return "redirect:/hotel/"+hotelId+"/distance";
   }

   @RequestMapping(value = "/{calinary}/{id}/update/form", method = RequestMethod.GET)
   public String updateCalinaryDistancesForm(@PathVariable("calinary")String calinaryType, @PathVariable("id")Long businessId, Model uiModel) {
      List<Distance> distances = businessService.findDistancesByCalinary(businessId);
      processDistances(businessId,distances,uiModel);
      uiModel.addAttribute("businessType", calinaryType);
      return "distance/update";
   }

   @RequestMapping(value = "/{calinary}/{id}/update", method=RequestMethod.POST)
   public String updateCalinaryDistances(@PathVariable("calinary")String calinaryType, @PathVariable("id") Long id, @Valid DistanceListModel distanceListModel, BindingResult bindingResult, Model uiModel) {
      if (bindingResult.hasErrors()) {
         return updateCalinaryDistancesForm(calinaryType, id, uiModel);
      }
      CulinaryCulture culinaryCulture = businessService.findCulinary(id);
      culinaryCulture.addAndUpdateDistances(distanceListModel.getDistances());
      businessService.saveBusiness(culinaryCulture);
      return "redirect:/"+calinaryType+"/"+id+"/distance";
   }

   private void processDistances(Long businessId, List<Distance> distances, Model uiModel){
      if (distances.size() == 0){
         distances.add(new Distance("nuibaden", 0));
         distances.add(new Distance("toathanhtayninh", 0));
         distances.add(new Distance("cholonghoa", 0));
         distances.add(new Distance("cuakhaumocbai", 0));
         distances.add(new Distance("runglichsu", 0));
         distances.add(new Distance("hodautieng", 0));
         distances.add(new Distance("cancutrunguong", 0));
         distances.add(new Distance("cuakhauxamat", 0));
         distances.add(new Distance("vuonquocgia", 0));
         distances.add(new Distance("longdienson", 0));
      }
      uiModel.addAttribute("distances", distances);
      uiModel.addAttribute("businessId", businessId);
   }
}
